package com.cl.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.bean.Emp;
import com.cl.dao.EmpDao;
import com.cl.service.EmpService;
import com.cl.utils.PageBean;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
	
	@Autowired
	private EmpDao empDao;
	
	@Override
	public PageBean getPageBean(DetachedCriteria dc, int page, int limit) {
		int count = empDao.getTotalCount(dc);
		PageBean pb = new PageBean(page, limit, count);
		List<Emp> list = empDao.getPageList(dc, pb.getStart(), pb.getPageSize());
		pb.setData(list);
		return pb;
	}

	@Override
	public void save(Emp emp) {
		empDao.save(emp);
	}

	@Override
	public List<Emp> findByPid(int p_id) {
		DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
		dc.add(Restrictions.eq("dept.p_id", p_id));
		return empDao.finByDetachedCriteria(dc);
	}

	@Override
	public void del(int e_id) {
		Emp emp = empDao.findById(e_id);
		emp.setDept(null);
		emp.setE_is(0);
		empDao.update(emp);
	}

	@Override
	public Emp findById(int e_id) {
		return empDao.findById(e_id);
	}

	@Override
	public void update(Emp emp) {
		empDao.update(emp);
	}

}
